package com.order.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.entity.Shop;
import com.common.entity.Shopcar;
import com.common.entity.TbBusiness;
import com.common.untils.CommonResult;
import com.order.mapper.ShopcarMapper;
import com.order.service.IShopcarService;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xdl
 * @since 2022-08-31
 */
@Service
public class ShopcarServiceImpl extends ServiceImpl<ShopcarMapper, Shopcar> implements IShopcarService {
    @Resource
    private ShopcarMapper shopcarMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    public CommonResult selecByIdShopCar(Integer currentPage, Integer pageSizes,String hometoken) {
        ValueOperations<String, String> opsForValue = redisTemplate.opsForValue();
        String s = opsForValue.get(hometoken);
        Map map = JSONObject.parseObject(s, Map.class);
        JSONObject client = (JSONObject) map.get("client");
        Integer id = client.getInteger("id");
        Page page = new Page<>(currentPage,pageSizes);
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("o.is_deleted",0);
        queryWrapper.eq("clientId",id);
        IPage<Map> shopCar =  shopcarMapper.selecByIdShopCar(page,queryWrapper);
        return new CommonResult(2000,"查询成功",shopCar);
    }

    @Override
    public CommonResult deleteById(Integer id) {
        int i = shopcarMapper.deleteById(id);
        if (i==1){
            return new CommonResult(2000,"删除成功",null);
        }
        return new CommonResult(5000,"删除失败",null);
    }

    //添加商品信息
    @Override
    public CommonResult inset(Shopcar shopcar,String hometoken) {
        ValueOperations<String, String> opsForValue = redisTemplate.opsForValue();
        String s = opsForValue.get(hometoken);
        Map map = JSON.parseObject(s, Map.class);
        JSONObject client = (JSONObject) map.get("client");
        Integer id = client.getInteger("id");
        //查询数据里面的商品id
        List<Shopcar> shopcars = shopcarMapper.selectList(null);
        for (Shopcar shopcar1 :shopcars) {
            if (shopcar1.getGsid().equals(shopcar.getGsid())) {
                QueryWrapper<Shopcar> wrapper=new QueryWrapper<>();
                wrapper.eq("id",shopcar1.getId());
                Shopcar sa=new Shopcar();
                sa.setGscounts(shopcar.getGscounts()+shopcar1.getGscounts());
                int update = shopcarMapper.update(sa, wrapper);
                return new CommonResult(2001,"修改成功",null);
            }
        }
        //添加信息
        Shopcar shopcar1=new Shopcar();
        shopcar1.setGscounts(shopcar.getGscounts());
        shopcar1.setGsid(shopcar.getGsid());
        shopcar1.setClientId(id);
        int insert = shopcarMapper.insert(shopcar1);
        if (insert !=0) {
            return new CommonResult(2000,"添加成功",null);
        }
        return new CommonResult(5000,"添加失败",null);
    }
}
